package com.wx.controller;

import com.wx.dto.BorrowPageQueryDTO;
import com.wx.dto.UserDTO;
import com.wx.dto.UserPageQueryDTO;
import com.wx.dto.UserPwd;
import com.wx.entity.Notice;
import com.wx.entity.User;
import com.wx.result.PageResult;
import com.wx.result.Result;
import com.wx.service.UserService;
import com.wx.vo.NoticeVO;
import com.wx.vo.UserLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*允许跨域请求*/
//@CrossOrigin
@RestController
@RequestMapping("/user")
@Slf4j
/* 用户控制类 */
public class UserController{
    @Autowired
    private UserService userService;

    /*新增账号*/
    @PostMapping
    public Result save(@RequestBody UserDTO userDTO){
        log.info("用户注册：{}",userDTO);
        userService.save(userDTO);
        return Result.success();
    }

    @GetMapping("/login")
    public Result login(UserDTO userDTO){
        log.info("用户登录：{}",userDTO);
        UserLoginVO user = userService.login(userDTO);

        //todo:添加jwt令牌
        return Result.success(user);
    }

    /*获取用户信息*/
    @GetMapping("/info")
    public Result getinfo(Integer id){
        log.info("获取用户信息：{}",id);
        User user = userService.getinfo(id);
        return Result.success(user);
    }

    /*更新密码*/
    @PutMapping("/updatepwd")
    public Result updatepwd(@RequestBody UserPwd userPwd){
        log.info("修改密码：{}",userPwd);
        userService.updatepwd(userPwd);
        return Result.success();
    }

    /*更新用户信息*/
    @PutMapping("/update")
    public Result update(@RequestBody UserDTO userDTO){
        log.info("更新用户信息：{}",userDTO);
        userService.update(userDTO);
        return Result.success();
    }

    /*获取用户列表*/
    @GetMapping("/list")
    private Result userlist(UserPageQueryDTO userPageQueryDTO){
        log.info("获取用户列表{}",userPageQueryDTO);
        PageResult pageResult = userService.userlist(userPageQueryDTO);
        return Result.success(pageResult);
    }

    /*用户获取通知*/
    @GetMapping("/notices")
    private Result usergetnotice(Notice notice){
        log.info("用户获取通知{}",notice);
        List<NoticeVO> list = userService.getnotice(notice);
        return Result.success(list);
    }

    /*用户收到通知*/
    @PostMapping("rnotice")
    private Result receive(@RequestBody Notice notice){
        log.info("用户收到通知{}",notice);
        userService.receive(notice);
        return Result.success();
    }

}
